import datetime,cgi
import sys
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
from os import environ
from models import *

class AddToPool(webapp.RequestHandler,db.Model):
	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		category=[]		
		allcategories=db.GqlQuery("SELECT * FROM projectCategory")
		for spcategory in allcategories:
			tmp=[]
			tmp.append(spcategory.categoryid)
			tmp.append(spcategory.categorytype)
			category.append(tmp)

		languages=[]		
		alllanguages=db.GqlQuery("SELECT * FROM languages")
		for language in alllanguages:
			tmp=[]
			tmp.append(language.languageid)
			tmp.append(language.language)
			languages.append(tmp)

		fieldnames=[]
		allfields=db.GqlQuery("SELECT * FROM projectFields")
		for fieldname in allfields:
			tmp=[]
			tmp.append(fieldname.fieldid)
			tmp.append(fieldname.fieldname)
			fieldnames.append(tmp)
		
		path = os.path.join(os.path.dirname(__file__),'addtopool.html')#use template
		self.response.out.write(template.render(path,{"category":category,"language":languages,"fieldname":fieldnames}))#using template
		
	def post(self):
		pname=self.request.get("projectname")
		pabstract=self.request.get("abstract")
		pcategory=str(self.request.get("category"))
		pfields=''
		planguages=''
		allfields=db.GqlQuery("SELECT * FROM projectFields")
		alllanguages=db.GqlQuery("SELECT * FROM languages")
		for language in alllanguages:
			if(self.request.get("l"+language.languageid)=='yes'):
				if(planguages!=''):
					planguages=planguages+","+language.languageid
				else:
					planguages=language.languageid
		for fieldname in allfields:
			if(self.request.get("f"+fieldname.fieldid)=='yes'):
				if(pfields!=''):
					pfields=pfields+","+fieldname.fieldid
				else:
					pfields=fieldname.fieldid
		projecttopool=projectPool(projectname=pname,languages=planguages,category=pcategory,abstract=pabstract,fieldnames=pfields,lock=False)
		key=projecttopool.put()
		idnum=key.id()
		projecttopool.poolprojectid=str(idnum)
		projecttopool.put()
		self.redirect("/admin")

application = webapp.WSGIApplication([('/addtopool', AddToPool)], debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
